package com.zj.utils.map;

import android.graphics.Point;
import android.util.Log;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author jiang zhu on 2019/10/21
 */
public class MapUtils {

    private static final String TAG = "MapUtils";
    private static final int DP_VALUE_POSITIVE = 45;
    private static final int DP_VALUE_NEGATIVE = -45;

    /**
     * 获取两经纬度点之间的直线距离
     */
    public static Double distance(double lat1, double lng1, double lat2, double lng2) {

        double R = 6370996.81;  //地球的半径
        //获取两点间x,y轴之间的距离
        double x = (lng2 - lng1) * Math.PI * R * Math.cos(((lat1 + lat2) / 2) * Math.PI / 180) / 180;
        double y = (lat2 - lat1) * Math.PI * R / 180;
        //得到两点之间的直线距离

        return Math.hypot(x, y);
    }

    /**
     * 获取两点之间经纬度的角度
     *
     * @param lat1 经度
     * @param lon1 纬度
     * @param lat2 /
     * @param lon2 /
     * @return 角度
     */
    public static double bearing(double lat1, double lon1, double lat2, double lon2) {
        double latitude1 = Math.toRadians(lat1);
        double latitude2 = Math.toRadians(lat2);
        double longDiff = Math.toRadians(lon2 - lon1);
        double y = Math.sin(longDiff) * Math.cos(latitude2);
        double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);
        return (Math.toDegrees(Math.atan2(y, x)) + 360) % 180;
    }


    public static List<Point> getPointLocation(Point p1, Point p2) {
        double k = (p2.y - p1.y) / (p2.x - p1.x);//求出斜率
        double c = p1.y - k * p1.x;//求出p1点的常数系数
        int size = 45;
        int sizeSquare = size * size;

        double sqrt = Math.sqrt(((-2) * p1.x) * ((-2) * p1.x) - 4 * (((k * p1.x + c) - p1.y) * ((k * p1.x + c) - p1.y) + p1.x * p1.x - sizeSquare));
        double x1 = ((-((-2) * p1.x) + sqrt) / 2);//运用求根公式，并且Math.sqrt()为开平方根
        double x2 = ((-((-2) * p1.x) - sqrt) / 2);//运用求根公式，并且Math.sqrt()为开平方根

        double sqrt1 = Math.sqrt(((-2) * p2.x) * ((-2) * p2.x) - 4 * (((k * p2.x + c) - p2.y) * ((k * p2.x + c) - p2.y) + p2.x * p2.x - sizeSquare));
        double x3 = ((-((-2) * p2.x) + sqrt1) / 2);//运用求根公式，并且Math.sqrt()为开平方根
        double x4 = ((-((-2) * p2.x) - sqrt1) / 2);//运用求根公式，并且Math.sqrt()为开平方根
        List<Point> pointList = new LinkedList<>();
        pointList.add(new Point((int) (x1 - p1.x), (int) (k * x1 + c - p1.y)));
        pointList.add(new Point((int) (x2 - p1.x), (int) (k * x2 + c - p1.y)));
        pointList.add(new Point((int) (x3 - p2.x), (int) (k * x3 + c - p2.y)));
        pointList.add(new Point((int) (x4 - p2.x), (int) (k * x4 + c - p2.y)));

        Log.e("TAG", "getPointLocations: " + "  size   " + size + "   " + pointList.toString());

        return pointList;
    }


    /**
     * 根据两点坐标求
     *
     * @param p1
     * @param p2
     * @return
     */
    public static List<Point> getPointLocations(Point p1, Point p2) {
        List<Point> pointList = new LinkedList<>();
        double k = (double) (p2.y - p1.y) / (double) (p2.x - p1.x);//求出斜率
        double c = p1.y - k * p1.x;//求出p1点的常数系数
        double k2 = -(1 / k);//垂直于这条线的斜率
        double c2 = p1.y + p1.x / k2;//垂直线的斜率常数系数
        int size = 45;//半径
        double c3 = size * (Math.sqrt(k * k + 1)) + c;
        double c4 = c - size * Math.sqrt(k * k + 1);


        //Log.e(TAG, "getPointLocations: k:"+k+"  c:"+c+"   k2:"+k2+"  c2:"+c2+"   c3:"+c3+"  c4:"+c4);

        double x1 = (c2 - c3) / (k + k2);
        double y1 = k * x1 + c3;

        double x2 = (c2 - c4) / (k + k2);
        double y2 = k * x2 + c4;

        double p2c = p2.y - k * p2.x;
        double p2c2 = p2.y + p2.x / k2;
        double p2c3 = size * Math.sqrt(k * k + 1) + p2c;
        double p2c4 = p2c - size * (Math.sqrt(k * k + 1));

        double x3 = (p2c2 - p2c3) / (k + k2);
        double y3 = k * x3 + p2c3;

        double x4 = (p2c2 - p2c4) / (k + k2);
        double y4 = k * x4 + p2c4;


        pointList.add(new Point((int) x1, (int) y1));
        pointList.add(new Point((int) x2, (int) y2));
        pointList.add(new Point((int) x3, (int) y3));
        pointList.add(new Point((int) x4, (int) y4));

        Log.e("TAG", "getPointLocations:ss 1 " + "  size   " + size + "   " + pointList.get(0).toString());
        Log.e("TAG", "getPointLocations:ss 2 " + "  size   " + size + "   " + pointList.get(1).toString());

        return pointList;
    }


    public static int getOffSetY(double degrees, String methodName) {
        if (methodName.equals("1")) {
            if (degrees > 0 && degrees < 90) {
                return (int) degrees / 2;
            } else if (degrees >= 90 && degrees < 180) {
                return 45 - (int) ((degrees - 90) / 2);
            } else if (degrees >= 180 && degrees < 270) {
                return (int) ((degrees - 180) / 2);
            } else {
                return 45 - (int) ((degrees - 180) / 2);
            }
        } else {
            if (degrees > 0 && degrees < 90) {
                return -((int) degrees / 2);
            } else if (degrees >= 90 && degrees < 180) {
                return -(45 - (int) ((degrees - 90) / 2));
            } else if (degrees >= 180 && degrees < 270) {
                return -((int) ((degrees - 180) / 2));
            } else {
                return -(45 - (int) ((degrees - 180) / 2));
            }
        }
    }

    public static int getOffSetX(double degrees, String methodName) {
        if (methodName.equals("1")) {
            if (degrees > 0 && degrees <= 90) {
                return (int) (45 - degrees / 2);
            } else if (degrees > 90 && degrees < 180) {
                return (int) ((degrees - 90) / 2 - 45);
            } else if (degrees >= 180 && degrees < 270) {
                return (int) (45 - (degrees - 180) / 2);
            } else {
                return (int) ((degrees - 180) / 2);
            }
        } else {
            if (degrees > 0 && degrees < 90) {
                return -((int) (45 - degrees / 2));
            } else if (degrees >= 90 && degrees < 180) {
                return -((int) ((degrees - 90) / 2));
            } else if (degrees >= 180 && degrees < 270) {
                return -((int) (45 - (degrees - 180) / 2));
            } else {
                return -((int) ((degrees - 180) / 2));
            }
        }
    }


    public static List<android.graphics.Point> getPointList(android.graphics.Point s_point, android.graphics.Point e_point, int dis, String methodName) {
        double k = (e_point.y - s_point.y) * 1.0 / (e_point.x - s_point.x) * 1.0;
        double a = Math.atan(k);
        double b = (180 * a) / 3.1415926;

        android.graphics.Point gpoint1;
        android.graphics.Point gpoint2;

        List<android.graphics.Point> list = new ArrayList<>();

        if (-45.0 < b && b < 45.0) {
            if (methodName.equals("1")) {
                gpoint1 = new android.graphics.Point(s_point.x, s_point.y - dis);
                gpoint2 = new android.graphics.Point(e_point.x, e_point.y - dis);
            } else {
                gpoint1 = new android.graphics.Point(s_point.x, s_point.y + dis);
                gpoint2 = new android.graphics.Point(e_point.x, e_point.y + dis);
            }
            list.add(gpoint1);
            list.add(gpoint2);
        } else {
            if (methodName.equals("1")) {
                gpoint1 = new android.graphics.Point(s_point.x - dis, s_point.y);
                gpoint2 = new android.graphics.Point(e_point.x - dis, e_point.y);
            } else {
                gpoint1 = new android.graphics.Point(s_point.x + dis, s_point.y);
                gpoint2 = new android.graphics.Point(e_point.x + dis, e_point.y);
            }
            list.add(gpoint1);
            list.add(gpoint2);
        }
        return list;
    }


}
